// pages/cityList/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    currentCity: '',
    originalList:[],//所有城市
    cityList: [],//符合条件渲染城市
    showAll: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.currentCity){
      this.setData({
        currentCity: options.currentCity
      })
    }
    if(options.showAll){
      this.setData({
        showAll: options.showAll
      })
    }
    this.getCityList();
  },
  getCityList: function(){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/tool/citylist',
      success: function(res){
        if(res.data.Code == 0){
          let cityList = JSON.parse(res.data.Data);
          that.setData({
            cityList: cityList,
            originalList: cityList
          })
        }
      }
    })
  },
  bindCity: function(e){
    this.setData({
      keyword: e.detail.value
    })
    this.searchCity();
  },

  searchCity: function(){
    let keyword = this.data.keyword;
    let cityList = this.data.originalList;
    let newList = [];
    for(let i = 0; i < cityList.length; i++){
      if(cityList[i].name.indexOf(keyword) != -1){
        newList.push(cityList[i]);
      }
    }
    this.setData({
      cityList: newList
    })
  },
  selCity: function(e){
    let city = e.currentTarget.dataset.city;
    this.setData({
      currentCity: city
    })
    let showAll = this.data.showAll;
    if(showAll == 1){
      wx.redirectTo({
        url: '/pages/branchStore/index?currentCity=' + city + '&showAll=1',
      })
    }else{
      wx.redirectTo({
        url: '/pages/branchStore/index?currentCity=' + city,
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})